/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelFormularioGeneral.java
 *
 * Created on 21-nov-2011, 10:39:23
 */
package androidutal;

import com.toedter.calendar.JDateChooser;
import java.awt.BorderLayout;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author blackmoon
 */
public class JPanelOfertaLaboral extends javax.swing.JPanel {
    private final JVentana ventana;
    private File file;
    private String fechaInicio;
    private String fechaFin;
    private boolean isBorrador;
    private  Date date;
    
    /** Creates new form JPanelFormularioGeneral */
    public JPanelOfertaLaboral(JVentana ventana) {
        initComponents();
        file=null;
        this.ventana = ventana;
        this.isBorrador = false;
        this.date=new Date();
        this.jFechaInicio.setDate(this.date);
        this.jFechaFin.setDate(this.date) ;
    }
    public boolean isBorrador() {
        return isBorrador;
    }
    public void setIsBorrador(boolean isBorrador) {
        this.isBorrador = isBorrador;
    }
    public String getFechaFin() {
        return fechaFin;
    }

    public void setFechaFin(String fechaFin) {
        this.fechaFin = fechaFin;
    }

    public String getFechaInicio() {
        return fechaInicio;
    }

    public void setFechaInicio(String fechaInicio) {
        this.fechaInicio = fechaInicio;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public JTextField getjAdjunto() {
        return jAdjunto;
    }

    public void setjAdjunto(JTextField jAdjunto) {
        this.jAdjunto = jAdjunto;
    }

    public JTextField getjCargo() {
        return jCargo;
    }

    public void setjCargo(JTextField jCargo) {
        this.jCargo = jCargo;
    }

    public JTextField getjDestino() {
        return jDestino;
    }

    public void setjDestino(JTextField jDestino) {
        this.jDestino = jDestino;
    }

    public JTextField getjEmpresa() {
        return jEmpresa;
    }

    public void setjEmpresa(JTextField jEmpresa) {
        this.jEmpresa = jEmpresa;
    }

    public JDateChooser getjFechaFin() {
        return jFechaFin;
    }

    public void setjFechaFin(String FechaFin) {
        this.jFechaFin.setDate(new Date(FechaFin));
    }

    public JDateChooser getjFechaInicio() {
        return jFechaInicio;
    }

    public void setjFechaInicio(String FechaInicio) {
        this.jFechaInicio.setDate(new Date(FechaInicio));
    }

    public JTextArea getjObservaciones() {
        return jObservaciones;
    }

    public void setjObservaciones(JTextArea jObservaciones) {
        this.jObservaciones = jObservaciones;
    }

    public JTextArea getjRequisitos() {
        return jRequisitos;
    }

    public void setjRequisitos(JTextArea jRequisitos) {
        this.jRequisitos = jRequisitos;
    }

    public JTextField getjSalario() {
        return jSalario;
    }

    public void setjSalario(JTextField jSalario) {
        this.jSalario = jSalario;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jEmpresa = new javax.swing.JTextField();
        jAdjunto = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jObservaciones = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jPublicar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jCargo = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jDestino = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jSalario = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jRequisitos = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jFechaInicio = new com.toedter.calendar.JDateChooser();
        jFechaFin = new com.toedter.calendar.JDateChooser();

        setBackground(java.awt.Color.black);
        setMinimumSize(new java.awt.Dimension(454, 386));
        setPreferredSize(new java.awt.Dimension(454, 386));

        jLabel1.setBackground(java.awt.Color.black);
        jLabel1.setForeground(java.awt.Color.white);
        jLabel1.setText("Empresa:");

        jLabel2.setBackground(java.awt.Color.black);
        jLabel2.setForeground(java.awt.Color.white);
        jLabel2.setText("Adjuntar:");

        jLabel3.setBackground(java.awt.Color.black);
        jLabel3.setForeground(java.awt.Color.white);
        jLabel3.setText("Observaciones:");

        jEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jEmpresaActionPerformed(evt);
            }
        });

        jAdjunto.setEditable(false);

        jObservaciones.setColumns(20);
        jObservaciones.setLineWrap(true);
        jObservaciones.setRows(5);
        jObservaciones.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jObservaciones);

        jButton1.setBackground(java.awt.Color.white);
        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPublicar.setBackground(java.awt.Color.white);
        jPublicar.setText("Publicar");
        jPublicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPublicarActionPerformed(evt);
            }
        });

        jButton3.setBackground(java.awt.Color.white);
        jButton3.setText("Borrador");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Oferta laboral");

        jButton4.setBackground(java.awt.Color.white);
        jButton4.setText("Adjuntar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel9.setBackground(java.awt.Color.black);
        jLabel9.setForeground(java.awt.Color.white);
        jLabel9.setText("Cargo:");

        jLabel10.setBackground(java.awt.Color.black);
        jLabel10.setForeground(java.awt.Color.white);
        jLabel10.setText("Destino Cargo:");

        jLabel11.setBackground(java.awt.Color.black);
        jLabel11.setForeground(java.awt.Color.white);
        jLabel11.setText("Salario:");

        jLabel5.setBackground(java.awt.Color.black);
        jLabel5.setForeground(java.awt.Color.white);
        jLabel5.setText("Requisitos:");

        jRequisitos.setColumns(20);
        jRequisitos.setLineWrap(true);
        jRequisitos.setRows(5);
        jRequisitos.setWrapStyleWord(true);
        jScrollPane2.setViewportView(jRequisitos);

        jLabel8.setBackground(java.awt.Color.black);
        jLabel8.setForeground(java.awt.Color.white);
        jLabel8.setText("Fecha post:");

        jLabel12.setBackground(java.awt.Color.black);
        jLabel12.setForeground(java.awt.Color.white);
        jLabel12.setText("Hasta:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jButton3)
                            .addComponent(jLabel9)
                            .addComponent(jLabel11)
                            .addComponent(jLabel10)
                            .addComponent(jLabel2)
                            .addComponent(jLabel8)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jFechaInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jAdjunto, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSalario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                            .addComponent(jEmpresa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPublicar)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1))
                            .addComponent(jCargo, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                            .addComponent(jDestino, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))
                        .addGap(19, 19, 19))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addComponent(jDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jAdjunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(jLabel3))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jPublicar)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jCargo, jDestino, jEmpresa, jSalario});

    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    if(this.isBorrador()){
        this.jButton3ActionPerformed(evt);
        this.ventana.getResumen().eliminarFila();
        if(jFechaInicio.getDate() != null && jFechaFin.getDate() != null){
            this.setVisible(false);
            ventana.add(ventana.getResumen(),BorderLayout.CENTER);
            ventana.getResumen().setVisible(true); 
        }
    }
    else{
        this.setVisible(false);
        ventana.add(ventana.getResumen(),BorderLayout.CENTER);
        ventana.getResumen().setVisible(true); 
    }
    this.desbloquear();
}//GEN-LAST:event_jButton1ActionPerformed

private void jEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jEmpresaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jEmpresaActionPerformed

private void jPublicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPublicarActionPerformed
// TODO add your handling code here:
    if(jFechaInicio.getDate() != null && jFechaFin.getDate() != null && !jEmpresa.getText().equals("")&&  !jCargo.getText().equals("") && !jDestino.getText().equals("") 
      && !jSalario.getText().equals("") && file!= null && !jRequisitos.getText().equals("") &&  !jObservaciones.getText().equals("")){
          if(this.isBorrador)
               this.ventana.getResumen().eliminarFila();
        fechaInicio = new SimpleDateFormat("dd/MM/yyyy").format(this.jFechaInicio.getDate()); 
        fechaFin = new SimpleDateFormat("dd/MM/yyyy").format(this.jFechaFin.getDate()); 

        ventana.getBaseDeDatos().addOfertaLaboral(jEmpresa.getText(), jCargo.getText(),
                jDestino.getText(), jSalario.getText(), fechaInicio,fechaFin, 
                file, jRequisitos.getText(), jObservaciones.getText(),false,this.ventana.getConfiguracionSubeYBaja().generaUrl(file.getName()));
        Date date=new Date();
        ventana.getResumen().insertarInfo((String)this.jCargo.getText(),date.toString(),"No" );
        this.setVisible(false);
        ventana.add(ventana.getResumen(),BorderLayout.CENTER);
        ventana.getResumen().setVisible(true);
        ventana.save();
        ventana.saveToWS();
        ventana.subirFTP(file);
    }
    else{
        JOptionPane.showMessageDialog(this, "Rellene todos los campos obligatorios");
    }
}//GEN-LAST:event_jPublicarActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
     final JFileChooser fc = new JFileChooser();
     
     int result= fc.showOpenDialog(this);
     fc.setVisible(true);
     if (result==JFileChooser.APPROVE_OPTION)
     {
        file = fc.getSelectedFile();
        this.jAdjunto.setText(file.getPath());
     }
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
        fechaInicio = new SimpleDateFormat("dd/MM/yyyy").format(this.jFechaInicio.getDate()); 
        fechaFin = new SimpleDateFormat("dd/MM/yyyy").format(this.jFechaFin.getDate()); 
        if (file!= null)
            ventana.getBaseDeDatos().addOfertaLaboral(jEmpresa.getText(), jCargo.getText(),
                jDestino.getText(), jSalario.getText(), fechaInicio,fechaFin, 
                file, jRequisitos.getText(), jObservaciones.getText(),true,this.ventana.getConfiguracionSubeYBaja().generaUrl(file.getName()));
        else
            ventana.getBaseDeDatos().addOfertaLaboral(jEmpresa.getText(), jCargo.getText(),
                jDestino.getText(), jSalario.getText(), fechaInicio,fechaFin, 
                file, jRequisitos.getText(), jObservaciones.getText(),true,"");
        
        ventana.getResumen().insertarInfo((String)this.jCargo.getText(),this.date.toString(),"Si" );
        this.setVisible(false);
        ventana.add(ventana.getResumen(),BorderLayout.CENTER);
        ventana.getResumen().setVisible(true);
        ventana.save();
}//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jAdjunto;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JTextField jCargo;
    private javax.swing.JTextField jDestino;
    private javax.swing.JTextField jEmpresa;
    private com.toedter.calendar.JDateChooser jFechaFin;
    private com.toedter.calendar.JDateChooser jFechaInicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextArea jObservaciones;
    private javax.swing.JButton jPublicar;
    private javax.swing.JTextArea jRequisitos;
    private javax.swing.JTextField jSalario;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    public void bloquear(){
        this.getjAdjunto().setEditable(false);
        this.getjCargo().setEditable(false);
        this.getjDestino().setEditable(false);
        this.getjEmpresa().setEditable(false);
        this.getjFechaFin().setEnabled(false);
        this.getjFechaInicio().setEnabled(false);
        this.getjObservaciones().setEditable(false);
        this.getjRequisitos().setEditable(false);
        this.getjSalario().setEditable(false);
        this.jButton3.setVisible(false);
        this.jButton4.setVisible(false);
        this.jPublicar.setVisible(false);
        
    }
    public void desbloquear(){
        this.getjAdjunto().setEditable(true);
        this.getjCargo().setEditable(true);
        this.getjDestino().setEditable(true);
        this.getjEmpresa().setEditable(true);
        this.getjFechaFin().setEnabled(true);
        this.getjFechaInicio().setEnabled(true);
        this.getjObservaciones().setEditable(true);
        this.getjRequisitos().setEditable(true);
        this.getjSalario().setEditable(true);
        this.jButton3.setVisible(true);
        this.jButton4.setVisible(true);
        this.jPublicar.setVisible(true);
    }
}
